// import Search from "antd/lib/transfer/search";
// import axios from "axios";
import {
  CHANGEMONEY,
  SEARCH,
  ADDCOM,
  CLAERORDER,
  REMOVEITEM,
} from "../const/cashier_const";
// import cookies from "react-cookies";

const initState = [];
export function cashier_order_reduce(perState = initState, action) {
  const { type, data } = action;
  console.log(data);
  switch (type) {
    case CHANGEMONEY:
      for (const key in perState) {
        if (Object.hasOwnProperty.call(perState, key)) {
          if (perState[key].rowID === data.changeRow) {
            perState[key].price = data.money;
            perState[key].count = data.count;
          }
        }
      }
      perState = [...perState];
      return perState;

    case ADDCOM:
      // console.log(data);
      for (const key in perState) {
        if (Object.hasOwnProperty.call(perState, key)) {
          if (perState[key].cid === data.cid) {
            perState[key].count++;
            return [...perState];
          }
        }
      }
      return [{ ...data, rowID: perState.length + 1, count: 1 }, ...perState];

    case CLAERORDER:
      return [];

    case REMOVEITEM:
      let newState = []
      for (const key in perState) {
        if (Object.hasOwnProperty.call(perState, key)) {
          const item = perState[key];
          if (item.rowID !== data) {
            newState.push(item)
          }
        }
      }

      for (const key in newState) {
        if (Object.hasOwnProperty.call(newState, key)) {
          const item = newState[key];
          item.rowID= newState.length-key
        }
      }

      return [...newState];
    default:
      return perState;
  }
}

const initSearchState = [];
export function cashier_search_reduce(perState = initSearchState, action) {
  let { type, data } = action;
  switch (type) {
    case SEARCH:
      // console.log(data);
      return data;

    default:
      return [];
  }
}
